package com.tuo51.evaluate.controller;


import com.google.common.collect.Maps;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.vo.users.StaffVO;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/eapi/profile")
@Api(value = "/eapi/profile", tags = "测评 - 用户信息")
public class ProfileController {

    @Autowired
    StaffService staffService;

    @Autowired
    MemVerificationCache cache;


    /**
     * 检查用户VIP状态
     * /api/user/vip_status
     * @param ticket
     * @return
     */
    @RequestMapping(value = "/vip_status", method = GET, produces = "application/json")
    public StatusResponse checkVip(@RequestHeader("ticket") String ticket) {
        String userId = cache.getStaffId(ticket);
        Staff user = staffService.getById(userId);

        return StatusResponse.success(StaffVO.of(user, staffService.staffOrgnization(user)));
    }

    /**
     * 更新用户信息
     * @param ticket
     * @param areaName
     * @param address
     * @param realName
     * @param phoneNumber
     * @return
     */
    @RequestMapping(value = "/userinfo", method = POST, produces = "application/json")
    public StatusResponse userinfo(@RequestHeader("ticket") String ticket,
                                   String areaName,
                                   String address,
                                   String areaCode,
                                   String areaCodeDesc,
                                   String areaPhone,
                                   String realName,
                                   String phoneNumber ) {

        String userId = cache.getStaffId(ticket);
        Staff user = staffService.getById(userId);


        staffService.editStaff(user, areaName, address, realName, areaCode, areaCodeDesc, areaPhone, phoneNumber);


        Map<String, Object> result = Maps.newHashMap();
        result.put("user", StaffVO.of(user, staffService.staffOrgnization(user)));

        return StatusResponse.success(result);
    }
}
